const {baseStaticURL,baseStaticPath} =  require("../config")
const Book =  require("../util/book")
const bookModel = require("../model/book")
module.exports = {
    //上传电子书
    async upload(ctx){
        //1.拿到电子书 2.将电子书上传到静态资源服务器 koaBody配置中实现

        //3.返回电子书的信息
        if(!ctx.request.files.book){
            ctx.throw(422,"没有图书资源")
        }
        /*文件名称(原文件名称) 和 文件路径(物理地址)可以从ctx.request.files.book中拿到*/
        let paths = ctx.request.files.book.path.split("\\");
        //电子书上传之后的文件名
        let newFileName = paths.pop();
        //电子书在静态资源服务器中的线上地址
        let bookUrl = `${baseStaticURL}/book/${newFileName}`;
        
        //返回电子书的信息(文件名称(服务器地址) 书名 作者 出版社 语言)
        //返回电子书的信息(根文件 文件路径 解压路径 封面路径)
        //返回电子书的信息(封面图片 目录)
        // book中携带前端需要的所有数据!!
        let book = new Book({
            ...ctx.request.files.book,
            bookUrl,
            newFileName
        });
        await book.parse()
        //删除所有的物理路径
        delete book.path;
        delete book.coverPath;
        delete book.unzipPath;
        ctx.body = {
            code:20000,
            book
        }
    },

    //新增电子书
    async add(ctx){
      try{
        let {
          title,
          creator,
          publisher,
          language,
          rootUrl,
          url,
          unzipURL,
          coverURL,
          name
        } = ctx.request.body;
        //查询是否有同一本书
        const books = await bookModel.find({title,creator,publisher})
        if(books.length > 0) ctx.throw(409,"该书已上传");
        //新增
        const book = await bookModel.create({
          title,
          creator,
          publisher,
          language,
          rootUrl,
          url,
          unzipURL,
          coverURL,
          name
        })
  
        ctx.body={
          code:20000,
          data:book
        }
      }catch(err){
        ctx.body={
          code:50001,
          message:err.message
        }
      }
    },

    //查询电子书
    async list(ctx){
      try{
        let {pagesize,pageno} = ctx.query;
        pagesize = +pagesize;
        pageno = +pageno;
        const skip = pagesize * (pageno-1)
        const books = await bookModel.find().skip(skip).limit(pagesize)
        ctx.body={
          code:20000,
          data:books
        }
      }catch(err){
        ctx.body={
          code:50001,
          message:err.message
        }
      }
    },

    //查询数据总条数
    async total(ctx){
      try{
        const total = await bookModel.countDocuments()
        ctx.body={
          code:20000,
          data:total
        }
      }catch(err){
        ctx.body={
          code:50001,
          message:err.message
        }
      }
    },

    //删除电子书
    async del(ctx){
      try{
        const bookId = ctx.query.bookId;
        const delBook = await bookModel.deleteOne({ _id:bookId })
        ctx.body={
          code:20000,
          data:delBook
        }
      }catch(err){
        ctx.body={
          code:50001,
          message:err.message
        }
      }
    },

    //根据id查找对应的数据
    async findBookById(ctx){
      try{
        const bookId = ctx.query.bookId;
        const book = await bookModel.findById(bookId);
        ctx.body={
          code:20000,
          data:book
        }
      }catch(err){
        ctx.body={
          code:50001,
          message:err.message
        }
      }
    },
    //更新电子书
    async update(ctx){
      try{
        let {
          _id,
          title,
          creator,
          publisher,
          language,
          rootUrl,
          url,
          unzipURL,
          coverURL,
          name
        } = ctx.request.body;
        
        const book = await bookModel.findByIdAndUpdate(_id,{
          title,
          creator,
          publisher,
          language,
          rootUrl,
          url,
          unzipURL,
          coverURL,
          name
        })

        ctx.body={
          code:20000,
          data:book
        }
      }catch(err){
        ctx.body={
          code:50001,
          message:err.message
        }
      }
    }
}